<template>
    <h2 class="center">登录</h2>
	<van-form @submit="onSubmit">
	  <van-cell-group inset>
	    <van-field
	      v-model="username"
	      name="username"
	      label="用户名"
	      placeholder="用户名"
	      :rules="[
			  { required: true, message: '请填写用户名' }
			]"
	    />
	    <van-field
	      v-model="password"
	      type="password"
	      name="password"
	      label="密码"
	      placeholder="密码"
	      :rules="[{ required: true, message: '请填写密码' }]"
	    />
	  </van-cell-group>
	  <div style="margin: 16px;">
	    <van-button round block type="primary" native-type="submit">
	      提交
	    </van-button>
	  </div>
	</van-form>
	<div class="right side-margin">
		<p>没有帐号了，去<router-link to="/regist">注册</router-link></p>
	</div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import { Toast } from 'vant';
  import 'vant/es/toast/style';
  import {login} from "@/api/user.ts";
  import router from '@/router'
  
  const username = ref("")
  const password = ref("")
  const onSubmit = (values) => {
  	console.log('submit', values);
  	login(values)
  		.then(res => {
  			if (res.code != 0) {
				Toast.fail('登录失败')
				console.log(res.message)
  			} else {
				Toast.success('登录成功')
				localStorage.setItem("username", res.data.username)
				router.push("/dashboard/home")
  			}
  		})
		.catch(err => {
			console.log(err)
		})

  };
</script>

<style scoped lang="scss">
</style>